<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.hnas.core.util.PropertyUtil "%>
<%@page import="com.hnas.core.dto.DataBean"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${pageTitle}</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/static/common/multiple/easyui.css"/>
<script type="text/javascript">
(function($){ 

    $.fn.UIdialog = $.fn.dialog; /* 把jquery-ui的dialog另存为UIdialog */ 

})(jQuery)
</script>
<style>
.h_top{display:block; border: 1px solid #dddddd;
       background: #fff;padding:20px 20px;}
.h_top_i{display:inline-block;margin:5px 20px;}
.h_btn{border: 1px solid #cfa972;background: #cfa972;color: #fff;height:33px;
       line-height:34px;padding:0 20px;cursor:pointer}
.m_tab td,.m_tab th{word-wrap:break-word;text-align:center;vertical-align:middle;}
</style>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/common/multiple/jquery.easyui.min.js"></script>
<script type="text/javascript">
	var myEditUrl   = '${pageContext.request.contextPath}/backend/commodit/showCommodit/';
	var myAddUrl	= '${pageContext.request.contextPath}/backend/generateMrchTwoCode/add';
	var mySaveUrl	= '${pageContext.request.contextPath}/backend/commodit/editCommodit/';
	var mydeleteUrl	= '${pageContext.request.contextPath}/backend/generateMrchTwoCode/disable/';
	
</script>
</head>
<body>
  <!-- 标题 -->
	<div class="titleBar m_b_20">
		<div class="tit fl b_l_e74a5f">
			<h3 class="cn">生成商户二维码</h3>
			<span class="en">Generate Mrch TwoCode</span>
		</div>
		<div class="fr"></div>
	</div>
  <!-- end 标题 -->
  <form method="post" action="${pageContext.request.contextPath }/backend/commodit/listCommodit/${dto.mrchId}" name="searchForm"  id="myForm">
  <input type="hidden" name="pageNo" value="${dto.pageNo}">
  <input type="hidden" name="totalCount" value="${dto.totalCount}">
  <!-- 查询 -->
		<div class="h_top m_b_20" >				
                    <div class="h_top_i">
                		   <span>每页显示</span>
                			<input style="width:50px;" class="input_style2" type="text" placeholder="" id="pageSize" name="pageSize" value="${dto.pageSize}"> 条
                        </div>
                		 <div class="h_top_i h_btn" id="searchData">
               					<span >查  询</span>             				
               				</div>
 
                		 <div class="h_top_i h_btn" id="addData">
               					<span >添  加</span>             				
               			</div>
               			<div class="h_top_i h_btn" id="backData">
               					<span onclick="history.back();return false;">返  回</span>             				
               			</div>
		       </div>
	<!-- end 查询 -->
</form>

<%
boolean incDisplayTable = true;
boolean incEditTable = true;
boolean incUploadDiv = false;
boolean incBatchDelete = false;
boolean incDisplayStatus = false;
String uploadTmp="merchantDevice.xls";
%>
<div class="m_b_20">
	<table class="tb_style05 m_tab" width="100%" style="table-layout:fixed;">
		<thead>
			<tr>
				<c:forEach items="${beans}" var="o">
				 <c:if test="${o.display && !o.hidden}"><th>${o.title}</th></c:if>
				</c:forEach>
				<th >操作</th>
			</tr>
		</thead>
		<tbody class="allData">
			<c:forEach items="${pg.list}" var="o">
				<tr>
					<c:forEach items="${beans}" var="o1">
						<c:if test="${o1.display && !o1.hidden}">
							<td style="word-wrap:break-word;">
								<%=String.valueOf(PropertyUtil.getValue(pageContext.getAttribute("o"),((DataBean)pageContext.getAttribute("o1")).getField())).replace("false","否").replace("true","是")%>
							</td>
						</c:if>
					</c:forEach>
					<td >
					    <a href="#" class="c_3366cc btn editData" id="${o.id }">编辑</a>&nbsp;
					    <a href="#" class="c_3366cc btn delData" id="${o.id }">删除</a>						
					</td>
				</tr>
			</c:forEach>
		</tbody>
	</table>
</div>
<div class="searpage" align="right">
	<div>
		${pg.pageStr}
	</div>
</div>

<!-- 添加start -->
<div class="addDataDiv" id="addDIV" title="添加商品">
	<form action="#" enctype="multipart/form-data" id="addFm" method="post" name="addFm" class="addFm" autocomplete="off" style="display:none;" >
		<input type="hidden" name="id"  value="0" >
		<input name="mrchId" type="hidden" value="${dto.mrchId }">
		<table >
			<tr>
				<td>
					<label>商品名称：</label>
				</td>
				<td>
					<input class="input_style2" width="198px;" name="name" id="addCommoditName">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
				<td>
					<label>商户原价：</label>
				</td>
				<td>
					<input id="addOldPrice" name="oldPrice" value="" style="width:169px;" class="input_style2">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
				<td>
					<label>宝岛通价格：</label>
				</td>
				<td>
					<input id="addPrice" name="price" value="" style="width:169px;" class="input_style2">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
					<td><label>商品图片：</label></td>
					<td><input type="hidden" name="picUrl" id="addCommoditPicInput">
						<img id="addImg" src=""  width="50px" height="50px">
						<input type="file" name="addCommoditPicFile" id="addCommoditPicFileInput"
						style="width: 200px" class="showContent" onchange="addChangeFile(this);"></td>
						
				</tr>
			<tr>
				<td>
					<label>商品描述：</label>
				</td>
				<td>
					<input id="addDesc" name="desc" value="" style="width:169px;" class="input_style2">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
      			<td colspan="2">
          			<p class="validateTips_deal" align="center"></p>
      			</td>
    		</tr>
		</table>
	</form>
</div>
<!-- 添加end -->

<!-- 编辑start -->
<div class="editDataDiv" id="editDIV" title="编辑商户生成二维码">
	<form action="#" enctype="multipart/form-data" id="editFm" method="post" name="editFm" class="addFm" autocomplete="off" >
		<input type="hidden" name="id" id="primaryId" value="0" >
		<input name="mrchId" type="hidden" value="${dto.mrchId }">
		<table >
			<tr>
				<td>
					<label>商品名称：</label>
				</td>
				<td>
					<input class="input_style2" width="198px;" name="name" id="editCommoditName">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
				<td>
					<label>商户原价：</label>
				</td>
				<td>
					<input id="editOldPrice" name="oldPrice" value="" style="width:169px;" class="input_style2">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
				<td>
					<label>宝岛通价格：</label>
				</td>
				<td>
					<input id="editPrice" name="price" value="" style="width:169px;" class="input_style2">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
					<td><label>商品图片：</label></td>
					<td><input type="hidden" name="picUrl" id="editCommoditPicInput">
						<img id="editImg" src=""  width="50px" height="50px">
						<input type="file" name="editCommoditPicFile" id="editCommoditPicFileInput"
						style="width: 200px" class="showContent" onchange="editChangeFile(this);"></td>
						
				</tr>
			<tr>
				<td>
					<label>商品描述：</label>
				</td>
				<td>
					<input id="editDesc" name="desc" value="" style="width:169px;" class="input_style2">
				    <font color="red">*</font>
				</td>
			</tr>
			<tr>
      			<td colspan="2">
          			<p class="validateTips_deal" align="center"></p>
      			</td>
    		</tr>
		</table>
	</form>
</div>
<!-- 编辑end -->
<script>
function addChangeFile(obj){
	var imgPath = URL.createObjectURL($(obj)[0].files[0]);
	$("#addImg").attr("src",imgPath);
	var fileName = $(obj).val().split("\\")[2];
	$("#addCommoditPicInput").val(fileName);
	console.log(fileName);
}

function editChangeFile(obj){
	var imgPath = URL.createObjectURL($(obj)[0].files[0]);
	$("#editImg").attr("src",imgPath);
	var fileName = $(obj).val().split("\\")[2];
	$("#editCommoditPicInput").val(fileName);
	console.log(fileName);
}

$("#searchData").click(function(){
	showMask3("../../images/loading-line.gif","查询中，请稍候...");
	/** 校验输入页数是否非法  **/
	var pageSize=$("#pageSize").val();
	var reg = new RegExp("^[0-9]*$");
	if($.trim(pageSize)!=""){
		if(!reg.test($.trim(pageSize))){
			hideMask();
			$.dialog.alert({title:"查询",content:"每页显示数请输入非负整数"});
			return false;
		}
	}
	if($.trim(pageSize)==""){
		hideMask();
		$.dialog.alert({title:"查询",content:"每页显示数不应许为空"});
		return false;
	}
	doSubmit();
});

function gotoJumpPage(){
	var jumpNum= $("#jumpNum").val();
	$("#search_page_size").val($("#id_page_size").val());
	gotoPage(jumpNum);
	return false;
}

$(function(){
	
	
})

function query(){
	showMask3("../../images/loading-line.gif","查询中，请稍候...");
	var f = document.searchForm;
	//f.totalCount.value = -1;
	f.submit();
}
	


/** 编辑 **/
$(".editData").click(function(){
	var id = $(this).attr("id");
	var editURL = myEditUrl + id;
	$.ajax({
		url : editURL,
		type: 'POST',
		error: function(XMLHttpRequest, textStatus, errorThrown){
			 $.dialog.alert({title:"保存",content:"加载数据异常。"});
		},
		success: function(data){
			console.log(data);
			$("#primaryId").val(data.id);
			$("#editCommoditName").val(data.name);
			$("#editOldPrice").val(data.oldPrice);
			$("#editPrice").val(data.price);
			$("#editDesc").val(data.desc);
			$("#editImg").attr("src","http://localhost:7731/ecard-manager"+data.picUrl);
		}
	});
	$("#editDIV").UIdialog("open");
});

/** 添加 **/	 
$("#addData").click(function(){
	clearAllData();
	$("#addDIV").UIdialog("open");
	$('#accountId').val('0');
});


function clearAllData() {
	$("#primaryId").val("0");
	$('#addMrchName').combobox('clear');

}

$("#addDIV").UIdialog({
	autoOpen : false,
	bgiframe: true, 
	height : 420,
	width : 350,
	modal : true,
	resizable : false,
	hide: '',
	show:'',
	zIndex:1000,
	buttons : {
		"保 存" : function() {
			//判空
			var bValid = getValid(1);
			if(bValid){
				showMask3("../../images/loading-line.gif","加载中，请稍候...");
				var formData = new FormData($("#addFm")[0]);  
				console.log(formData);
				$.ajax({
					url : "${pageContext.request.contextPath }/backend/commodit/addCommodit",
					type : 'POST',
					data : formData,
					async: false,    
		            cache: false,   
		            contentType: false, //不设置内容类型  
		            processData: false, //不处理数据  
		            success:function(data){  
		            	$.dialog.alert({title:"保存",content:data.errorMessage,callback:"query();"});
						clearAllData();
		            },  
		            error:function(){  
		                alert("上传失败！");  
		            }  
				});  
			}
		},
		"取 消" : function() {
			$(this).UIdialog("close");	
			clearAllData();
		}
	},
	close : function() {
		clearAllData();
	}
});

$("#editDIV").UIdialog({
	autoOpen : false,
	bgiframe: true, 
	height : 420,
	width : 350,
	modal : true,
	resizable : false,
	hide: '',
	show:'',
	zIndex:1000,
	buttons : {
		"保 存" : function() {
			//判空
			var bValid = getValid(2);
			if(bValid){
				var formData = new FormData($("#editFm")[0]);  
				showMask3("../../images/loading-line.gif","加载中，请稍候...");
				$.ajax({
					url : "${pageContext.request.contextPath }/backend/commodit/editCommodit",
					type : 'POST',
					data : formData,
					async: false,    
		            cache: false,   
		            contentType: false, //不设置内容类型  
		            processData: false, //不处理数据  
		            success:function(data){  
		            	$.dialog.alert({title:"保存",content:data.errorMessage,callback:"query();"});
						clearAllData();
		            },  
		            error:function(){  
		                alert("上传失败！");  
		            }  
				});  
			}
		},
		"取 消" : function() {
			$(this).UIdialog("close");	
			clearAllData();
		}
	},
	close : function() {
		//allFields.val("").removeClass("ui-state-error");
		clearAllData();
	}
});

/** 删除 **/
$(".delData").click(function(){
	var id = $(this).attr("id");
	 $.dialog.confirm({title:"删除",content:"您确定要删除吗",callback:"deleteData('"+id+"');"});
});

//删除
function deleteData(id){
	 $.ajax({
			url : "${pageContext.request.contextPath }/backend/commodit/disableCommodit/"+id,
			type : 'POST',
			error : function(XMLHttpRequest, textStatus, errorThrown){
				$.dialog.alert({title:"删除",content:"加载数据异常"});
			},
			success: function(data){
				if(data.result==1) {
				     $.dialog.alert({title:"删除",content:"删除成功"});
				     //setTimeout("query();",1000);
				     query();
				} else {
					$.dialog.alert({title:"删除",content:"删除失败"});	
				}
			}
		});  
}

function getValid(type){
	
	/* if(isNull(mrchName)) {
		$.dialog.alert({title:"保存",content:"商户名称不能为空！"});
		return false;
	}
	if(isNull(mrchId)) {
		$.dialog.alert({title:"保存",content:"商户编号不能为空！"});
		return false;
	} */
	return true;
}

</script>
</body>
</html>